package com.zjut.an.html.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.zjut.an.html.manager.HtmlManager;
import com.zjut.an.news.dao.AnNews;
import com.zjut.an.news.manager.AnNewsManager;
import com.zjut.an.util.system.Status;
import com.zjut.an.util.tools.HTMLCreat;
import com.zjut.an.webclass.dao.AnWebclass;
import com.zjut.an.webclass.manager.AnWebclassManager;
import com.zjut.zs.userstatus.dao.ZsUserStatus;
import com.zjut.zs.userstatus.manager.ZsUserStatusManager;

public class HtmlAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware {

	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	
	private AnNews obj;
	private Map<String , Object> dataMap;
	private int offset = 0, limit = 10;
	private String order;
	private String sort;
	private String search;
	private String rowstr;
	
	@Resource(name = "AnNewsManager")
	private AnNewsManager manager;
	
	@Resource(name = "AnWebclassManager")
	private AnWebclassManager wmanager;
	
	@Resource(name = "HtmlManager")
	private HtmlManager hmanager;
	
	@Resource(name = "HTMLCreat")
	private HTMLCreat htmlCreate;

	
	public String htmlIndex() {
		hmanager.setHttp(request, response);
		hmanager.CreatINDEX();
		return SUCCESS;
	}
	
	
	public String htmlAnWebclass() {
		dataMap = new HashMap<String, Object>();
		AnWebclass webclass = wmanager.getData(Integer.parseInt(request.getParameter("wcId")));
		hmanager.setHttp(request, response);
		hmanager.CreatINDEX();
		hmanager.CreatWebclass(webclass);
		List list = manager.getPubDatasByWcId(1, 100, "neId", "desc", webclass).getObjects();
		for (int i=0; i<list.size(); i++) {
			hmanager.CreatNews((AnNews)list.get(i));
		}	
		dataMap.put("flag", "success");
		return SUCCESS;
	}

	public String htmlClassWeb() {
		dataMap = new HashMap<String, Object>();
		hmanager.setHttp(request, response);
		hmanager.CreatINDEX();
		List wlist = wmanager.getDatas();
		for (int i=0; i<wlist.size(); i++) {
			AnWebclass webclass = (AnWebclass)wlist.get(i);
			//System.out.println(i+" creat webclass:"+webclass.getWcName() +" wcId:"+ webclass.getWcId() +"  size:"+wlist.size());
			hmanager.CreatWebclass(webclass);			
						
		}
		dataMap.put("flag", "success");
		return SUCCESS;
	}
	
	public String htmlAllWeb() {
		dataMap = new HashMap<String, Object>();
		hmanager.setHttp(request, response);
		hmanager.CreatINDEX();
		List wlist = wmanager.getDatas();
		for (int i=0; i<wlist.size(); i++) {
			AnWebclass webclass = (AnWebclass)wlist.get(i);
			//System.out.println(i+" creat webclass:"+webclass.getWcName() +" wcId:"+ webclass.getWcId() +"  size:"+wlist.size());
			hmanager.CreatWebclass(webclass);			
			List list = manager.getPubDatasByWcId(1, 100, "neId", "desc", webclass).getObjects();
			if (list.size()>0) {
				for (int j=0; j<list.size(); j++) {
					hmanager.CreatNews((AnNews)list.get(j));
				}
			}				
		}
		dataMap.put("flag", "success");
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
		session = request.getSession();
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		response = arg0;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public AnNews getAl() {
		return obj;
	}

	public void setAl(AnNews obj) {
		this.obj = obj;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String getRowstr() {
		return rowstr;
	}

	public void setRowstr(String rowstr) {
		this.rowstr = rowstr;
	}

	public AnNewsManager getManager() {
		return manager;
	}

	public void setManager(AnNewsManager manager) {
		this.manager = manager;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

}
